﻿using System;

namespace Gmantis.Controls.Chart.Extended
{
    public class YFunctionSeries : FunctionSeries
    {
        // Fields
        private string _codeError = "";
        private Func<double, double> _func;
        private string _funcJS;
        private bool _validCode = true;

        // Methods
        public override void UpdateData()
        {
            if ((this._func != null) || !string.IsNullOrEmpty(this._funcJS))
            {
                int sampleCount = base.SampleCount;
                double[] numArray = new double[sampleCount];
                double[] numArray2 = new double[sampleCount];
                double num2 = (base.Max - base.Min) / ((double)(sampleCount - 1));
                for (int i = 0; i < sampleCount; i++)
                {
                    numArray[i] = base.Min + (num2 * i);
                    if (this._validCode)
                    {
                        string errmsg = "";
                        numArray2[i] = base.CalculateValue(this._func, this._funcJS, numArray[i], out errmsg);
                        this._codeError = errmsg;
                        if (!string.IsNullOrEmpty(errmsg))
                        {
                            this._validCode = false;
                        }
                    }
                    else
                    {
                        numArray2[i] = double.NaN;
                    }
                }
                base.ValuesSource = numArray2;
                base.XValuesSource = numArray;
            }
        }

        // Properties
        public Func<double, double> Function
        {
            get
            {
                return this._func;
            }
            set
            {
                if (this._func != value)
                {
                    this._func = value;
                    this._validCode = true;
                    this.UpdateData();
                }
            }
        }

        public string FunctionCode
        {
            get
            {
                return this._funcJS;
            }
            set
            {
                if (this._funcJS != value)
                {
                    this._funcJS = value;
                    this._validCode = true;
                    this._codeError = base.GetErrorString(this._funcJS);
                    this.UpdateData();
                }
            }
        }

        public string FunctionCodeError
        {
            get
            {
                return this._codeError;
            }
            private set
            {
                if (this._codeError != value)
                {
                    this._codeError = value;
                }
            }
        }
    }
}
